//app.js
const mrhhttp = require('./network/mrhhttp.js')
const {
  init
} = mrhhttp

const regeneratorRuntime = require("./runtime.js"); //参考 https://juejin.im/post/5d366067e51d4550723b14cf
const MRDCommonConst = require('./utils/CommonConst.js')
const config = require('./config/index.js')
const CommonHttp = require('./network/CommonHttp.js')
const ShopHttp = require('./network/ShopHttp.js')
const CatagoryHttp = require('./network/CatagoryHttp.js')
const DeviceHttp = require('./network/DeviceHttp.js')
const FloorHttp = require('./network/FloorHttp.js')
const GoodsHttp = require('./network/GoodsHttp.js')
const FittingHttp = require('./network/FittingHttp.js')
const CashierHttp = require('./network/CashierHttp.js')
const UserHttp = require('./network/UserHttp.js')
const VisitorHttp = require('./network/VisitorHttp.js')
const MerchantHttp = require('./network/MerchantHttp.js')
const MerchantUserHttp = require('./network/MerchantUserHttp.js')
const RoleHttp = require('./network/RoleHttp.js')
const MerchantInviteCodeHttp = require('./network/MerchantInviteCodeHttp.js')
const BlackHttp = require('./network/BlackHttp.js')
const StaffHttp = require('./network/StaffHttp.js')
const MemberHttp = require('./network/MemberHttp.js')
const Util = require('./utils/util.js')
const QueryTime = require('./utils/QueryTime')
const GuestTypeObj = require('./utils/guestType.js')
const QQMapWX = require('./utils/qqmap-wx-jssdk')
App({

  onLaunch: async function () {
    init && init(this)
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        // env 参数说明：
        //   env 参数决定接下来小程序发起的云开发调用（wx.cloud.xxx）会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境（第一个创建的环境）
        // env: 'my-env-id',
        traceUser: true,
        env: 'test-7gl5z2za83ab0bd8'
      })
    }

    this.globalData = {
      cloud: wx.cloud
    }
    const res = await UserHttp.loadList();
    debugger
  },
  getToken: function () {
    return ''
  },
  // 登录
  loginInSystem: async function (path) {
    //该函数获取所有栈内的路由
    let pages = getCurrentPages();
    //数组中最后一个即当前路由，options是参数
    const targetUrl = config.loginPath;
    const currentUrls = pages.filter(v => v && v.route).map(v => v.route)
    if (currentUrls.length > 0) {
      this.lastPage = currentUrls[0];
    }
    debugger
    if (currentUrls.indexOf(targetUrl) === -1) {
      wx.navigateTo({
        url: `${targetUrl}`
      })
    }
  },
  popMethod: function (e) {
    let pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack();
    } else {
      this.reLaunch()
    }
  },
  reLaunch: function () {
    if (this.lastPage) {
      let pages = getCurrentPages();
      const currentUrls = pages.filter(v => v && v.route).map(v => v.route)
      const index = currentUrls.indexOf(this.lastPage);
      if (index != -1) {
        const delta = currentUrls.length - index;
        wx.navigateBack({
          delta: delta,
          success: res => {
            console.log(res);
            let affterPages = getCurrentPages();
            const aa = affterPages[affterPages.length - 1];
            aa && aa.onLoad && aa.onLoad()
          }
        })
      } else {
        wx.reLaunch({
          url: config.homePage
        })
      }
      this.lastPage = null;
    } else {
      wx.reLaunch({
        url: config.homePage
      })
    }


  },
  QueryTime: function () {
    return QueryTime
  },
  MRDCommonConst: function () {
    return MRDCommonConst;
  },
  lastChoiceInstance: function () {
    return wx.getStorageSync(MRDCommonConst.lastChoiceInstanceKey)
  },
  GuestTypeObj: function () {
    return GuestTypeObj
  },
  QQMapWX: function () {
    return QQMapWX;
  },

  config: function () {
    return config;
  },
  // config里的信息
  homePage: function () {

    return config.homePage
  },
  baseURL: function () {
    return config.baseURL
  },
  imageRoot: function () {
    return config.imageRoot
  },
  Util: function () {
    return Util
  },
  getMerchantId: function () {
    return wx.getStorageSync(MRDCommonConst.merchantIdKey)
  },
  getToken: function () {
    return wx.getStorageSync(MRDCommonConst.loginTokenKey)
  },
  // 请求参数
  MerchantHttp: function () {
    return MerchantHttp;
  },
  MerchantUserHttp: function () {
    return MerchantUserHttp
  },
  CommonHttp: function () {
    return CommonHttp;
  },
  FloorHttp: function () {
    return FloorHttp
  },
  DeviceHttp: function () {
    return DeviceHttp;
  },
  ShopHttp: function () {
    return ShopHttp;
  },
  CatagoryHttp: function () {
    return CatagoryHttp;
  },
  GoodsHttp: function () {
    return GoodsHttp
  },
  FittingHttp: function () {
    return FittingHttp
  },
  CashierHttp: function () {
    return CashierHttp
  },
  UserHttp: function () {
    return UserHttp
  },
  CommonHttp: function () {
    return CommonHttp;
  },
  VisitorHttp: function () {
    return VisitorHttp
  },
  RoleHttp: function () {
    return RoleHttp
  },
  BlackHttp: function () {
    return BlackHttp
  },
  MemberHttp: function () {
    return MemberHttp
  },
  StaffHttp: function () {
    return StaffHttp
  },
  MerchantInviteCodeHttp: function () {
    return MerchantInviteCodeHttp
  },

  //重写分享方法
  overShare: function () {
    //监听路由切换
    //间接实现全局设置分享内容
    wx.onAppRoute(function (res) {})
  },
})